package patterns.interpreter;

/**
 * Created by lenovo on 2016/11/30 0030.
 * 解释器演示
 */
public class InterpreterPatternDemo {
    //规则，robert和john是男性
    public static IExpression getMaleExpression(){
        IExpression robert = new TerminalExpression("Robert");
        IExpression john = new TerminalExpression("John");
        return new OrExpression(robert,john);
    }
    //规则，julie 是已婚女性
    public static IExpression getMarriedWomanExpression(){
        IExpression julie = new TerminalExpression("Julie");
        IExpression married = new TerminalExpression("Married");
        return new AndExpression(julie,married);
    }
    public static void main(String[] args){
        IExpression isMale = getMaleExpression();
        IExpression isMarriedWoman = getMarriedWomanExpression();
        //就是用context的上下文，去匹配定义的规则，若满足，则是符合指令。
        System.out.println("John is male ? "+isMale.interpret("John"));
        System.out.println("Julie is a married woman ? "+isMarriedWoman.interpret("Married Julie"));
    }
}
